package com.kingsoft.dc.khaos.module.mpp.sqlbuilder;

import com.kingsoft.dc.khaos.module.mpp.model.ColumnEntity;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * 贴源全量求增量sql构建.
 */
public class GetAddSqlBuilder {

    /**
     * 构建全量求增量sql语句,源档为增量
     *
     * @param current    当前表
     * @param history    历史表
     * @param keyList    主键列表
     * @param srcKeyList 指定字段信息
     * @return
     */
    public static String getAddSelectSql(String current, String history, List<String> keyList, List<ColumnEntity> srcKeyList, String filter) {
        System.out.println("GetAddSqlBuilder getAddSelectSql start...");
        String currentAlias = "c";
        String historyAlias = "h";
        String currentFilter = "";
        if (null != filter && !filter.equals("")) {
            currentFilter = "(SELECT * FROM " + current + " WHERE " + filter + ")";
        } else {
            currentFilter = current;
        }
        //String joinKeys = SqlBuilderUtils.joinKeys(currentAlias, historyAlias, keyList);
        String linkedKeysJoinSql = "";
        if (srcKeyList != null && srcKeyList.size() > 0) {
            linkedKeysJoinSql = SqlBuilderUtils.replaceNull(currentAlias, historyAlias, srcKeyList);
        }
        String pKeysJoinSql = "";
        if (keyList != null && keyList.size() > 0) {
            pKeysJoinSql = SqlBuilderUtils.joinKeys(currentAlias, historyAlias, keyList);
        }
        String onSql = "";
        if (linkedKeysJoinSql.isEmpty()) {
            onSql = pKeysJoinSql;
        } else if (pKeysJoinSql.isEmpty()) {
            onSql = linkedKeysJoinSql;
        } else {
            onSql = pKeysJoinSql + " AND " + linkedKeysJoinSql;
        }
        String where = SqlBuilderUtils.keyIsNull(keyList, historyAlias);
        String selectSql = String.format("SELECT %s.* FROM %s as %s  LEFT JOIN %s as %s ON %s  WHERE %s", currentAlias, currentFilter, currentAlias, history, historyAlias, onSql, where);
        System.out.println("GetAddSqlBuilder getAddSelectSql end...");
        return selectSql;
    }


    /**
     * 构建全量求增量sql语句,源档为减量
     *
     * @param current 当前表
     * @param history 历史表
     * @param keyList 主键列表
     *                //* @param srcKeyList 指定字段信息
     * @return
     */
    public static String getDelSelectSql(String current, String history, List<String> keyList, String filter) {
        System.out.println("GetAddSqlBuilder getDelSelectSql start...");
        String currentAlias = "c";
        String historyAlias = "h";
        String currentFilter = "";
        if (null != filter && !filter.equals("")) {
            currentFilter = "(SELECT * FROM " + current + " WHERE " + filter + ")";
        } else {
            currentFilter = current;
        }
        String joinKeys = SqlBuilderUtils.joinKeys(historyAlias, currentAlias, keyList);
        //String replaceNull = srcKeyList;
        String where = SqlBuilderUtils.keyIsNull(keyList, currentAlias);
        String selectSql = String.format("SELECT %s.* FROM %s as %s LEFT JOIN %s as %s ON %s WHERE %s", historyAlias, history, historyAlias, currentFilter, currentAlias, joinKeys, where);
        System.out.println("GetAddSqlBuilder getDelSelectSql end...");
        return selectSql;
    }

}
